package com.zhc.tools.demo.service;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.google.common.net.MediaType;
import com.zhc.tools.common.util.HttpClientUtil;
import com.zhc.tools.core.fileimport.IDealSingleService;
import com.zhc.tools.core.fileimport.domain.ImportLogObject;

/**
 * 说明：修改仓库信息 处理单条请求接口实现
 * @author zhc
 */
@Service
public class ImportWarehouseStrategyService implements IDealSingleService {
	
	protected final Logger logger = Logger.getLogger(this.getClass());

	@Override
	public boolean preData(ImportLogObject importLogObject) {
		
		Map<Integer, Object> dataMap = importLogObject.getDataMap();
		
		String code = (String) dataMap.get(0);
		
		Map<String, Object> paramMap = new HashMap<String, Object>();
		
		paramMap.put("partner_code", code);
		paramMap.put("province", dataMap.get(2));
		paramMap.put("city", dataMap.get(3));
		paramMap.put("county", dataMap.get(4));
		paramMap.put("address", dataMap.get(5));
		
		importLogObject.setCode(code);
		importLogObject.setData(paramMap);
		return true;
	}

	@Override
	public boolean execute(ImportLogObject importLogObject) {
		
		try {
			String url = "http://api.cis.7lk.cn/api/stock/pop/updatePartner";
			
			String content = JSON.toJSONString(importLogObject.getData());
			this.logger.info(String.format("调用CIS接口PopShippablePharmacyImpl URL【%s】 入参：%s", url, content));
			String result = HttpClientUtil.postForObject(url, MediaType.JSON_UTF_8.toString(), content);
			this.logger.info(String.format("调用CIS接口返回结果：%s", result));
		} catch (Exception e) {
			this.logger.error("调用CIS接口updatePartner异常：", e);
		}
		return true;
	}
	
}
